package whf.file.entity;

import org.apache.struts.upload.FormFile;

import whf.file.Constants;
import whf.framework.util.StringUtils;

/**
 * @author wanghaifeng
 * @create Nov 26, 2006 2:44:50 PM 
 * 
 */
public class File extends whf.framework.entity.AbstractEntity implements whf.framework.file.IFile {

	private static final long serialVersionUID = 0;

	/**
	 * 所处的文件夹，如果null，表示顶层文件
	 * @property whf.file.entity.Folder:folder
	 */
	private whf.file.entity.Folder folder;

	private java.sql.Blob content;

	/**
	 * 存储文件的物理路径
	 * @property String:phycialPath
	 */
	private String phycialPath;

	/**
	 * 是否保存到数据库
	 * @property int:saveInDb
	 */
	private boolean saveInDb;

	/**
	 * 文件类型，缺省的可以通过文件的扩展名获取
	 * @property whf.file.entity.Type:type
	 */
	private whf.file.entity.Type type;

	/**
	 * 文件大小
	 * @property int:size
	 */
	private int size;

	/**
	 * 如果是图片，图片宽度
	 * @property int:imageWidth
	 */
	private int imageWidth;

	/**
	 * 如果是图片，图片的高度
	 * @property int:imageHeight
	 */
	private int imageHeight;

	/**
	 * 当发生文件进行了复制或者新版本时指向的源文件
	 * @property whf.file.entity.File:source
	 */
	private whf.file.entity.File source;

	/**
	 * 文件的版本
	 * @property String:version
	 */
	private String version;

	/**
	 * 扩展名
	 * @property String:ext
	 */
	private String ext;
	
	/**
	 * 文件
	 * @property FormFile:phycialPathFile
	 */
	private FormFile phycialPathFile;
	// ---------- methods ----------

	public void setFolder(whf.file.entity.Folder folder) {
		this.folder = folder;
	}

	public whf.file.entity.Folder getFolder() {
		return this.folder;
	}

	public void setContent(java.sql.Blob content) {
		this.content = content;
	}

	public java.sql.Blob getContent() {
		return this.content;
	}

	public void setPhycialPath(String phycialPath) {
		this.phycialPath = phycialPath;
	}

	public String getPhycialPath() {
		return this.phycialPath;
	}

	public void setSaveInDb(boolean saveInDb) {
		this.saveInDb = saveInDb;
	}

	public boolean isSaveInDb() {
		return this.saveInDb;
	}

	public void setType(whf.file.entity.Type type) {
		this.type = type;
	}

	public whf.file.entity.Type getType() {
		return this.type;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public int getSize() {
		return this.size;
	}

	public void setImageWidth(int imageWidth) {
		this.imageWidth = imageWidth;
	}

	public int getImageWidth() {
		return this.imageWidth;
	}

	public void setImageHeight(int imageHeight) {
		this.imageHeight = imageHeight;
	}

	public int getImageHeight() {
		return this.imageHeight;
	}

	public void setSource(whf.file.entity.File source) {
		this.source = source;
	}

	public whf.file.entity.File getSource() {
		return this.source;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public String getVersion() {
		return this.version;
	}

	//
	
	/**
	 * @return Returns the ext.
	 */
	public String getExt() {
		return ext;
	}

	/**
	 * @param ext The ext to set.
	 */
	public void setExt(String ext) {
		this.ext = ext;
	}

	/**
	 * @return Returns the phycialPathFile.
	 */
	public FormFile getPhycialPathFile() {
		return phycialPathFile;
	}

	/**
	 * @param phycialPathFile The phycialPathFile to set.
	 */
	public void setPhycialPathFile(FormFile phycialPathFile) {
		this.phycialPathFile = phycialPathFile;
	}

	/**
	 * 生成图片对应类型的图片文件
	 * @modify wanghaifeng Nov 26, 2006 3:00:49 PM
	 * @param fileName
	 * @param type
	 * @return
	 */
	public static String convertIconFileName(String fileName, String type){
		String fn = fileName.substring(0, fileName.lastIndexOf("."));
		String ext = fileName.substring(fileName.lastIndexOf(".") + 1);
		return fn+"."+type+"."+ext;
	}
	
	/**
	 * 给定文件名是否是图片文件
	 * @modify wanghaifeng Nov 26, 2006 3:00:32 PM
	 * @param fileName
	 * @return
	 */
	public static boolean isImageFile(String fileName){
		if(StringUtils.isEmpty(fileName)) return false;
		int index = fileName.lastIndexOf(".");
		if(index >= 0){
			String ext = fileName.substring(index + 1);
			for(int i=0; i< Constants.IMAGE_FILE_EXTS.length; i++){
				if(StringUtils.equalsIgnoreCase(ext, Constants.IMAGE_FILE_EXTS[i])){
					return true;
				}
			}
		}
		return false;
	}
	
	/**
	 * 获取文件小图标
	 * @modify wanghaifeng Nov 26, 2006 2:59:47 PM
	 * @return
	 */
	public String getSmallIcon(){
		if(isImageFile(this.phycialPath)){
			return convertIconFileName(this.phycialPath, Constants.ICON_TYPE_SMALL);
		} else if(this.type != null){
			return this.type.getSmallIcon();
		} else {
			return null;
		}
	}
	
	/**
	 * 获取文件小图标
	 * @modify wanghaifeng Nov 26, 2006 2:59:47 PM
	 * @return
	 */
	public String getMiddleIcon(){
		if(isImageFile(this.phycialPath)){
			return convertIconFileName(this.phycialPath, Constants.ICON_TYPE_MIDDLE);
		} else if(this.type != null){
			return this.type.getMiddleIcon();
		} else {
			return null;
		}
	}
	
	/**
	 * 获取图片文件的大图标
	 * @modify wanghaifeng Nov 26, 2006 3:05:05 PM
	 * @return
	 */
	public String getLargeIcon(){
		if(isImageFile(this.phycialPath)){
			return convertIconFileName(this.phycialPath, Constants.ICON_TYPE_LARGE);
		} else if(this.type != null){
			return this.type.getLargeIcon();
		} else {
			return null;
		}
	}

	/* (non-Javadoc)
	 * @see whf.framework.file.IFile#getFileFolder()
	 */
	public whf.framework.file.IFolder getFileFolder() {
		return this.folder;
	}

	/* (non-Javadoc)
	 * @see whf.framework.file.IFile#getFileType()
	 */
	public whf.framework.file.IType getFileType() {
		return this.type;
	}
	
	
}